package com.smartstate.iot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smartstate.iot.entity.IotDataDistribution;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface IotDataDistributionMapper extends BaseMapper<IotDataDistribution> {

     /**
      *   通过dataId和originalTarget获取映射关系的id
      * @param dataId
      * @param originalTarget
      * @return
      */
     @Select("select * FROM iot_data_distribution WHERE data_id = #{originalTarget} AND target_data_id = #{dataId}")
     List<IotDataDistribution> getRelationship(String dataId, String originalTarget);

     /**
      *   通过dataId获取其分发目标
      * @param dataId
      * @return
      */
     @Select("select id,data_id dataId,attribute_type attributeType,weight,target_data_id distributionTarget FROM iot_data_distribution WHERE data_id = #{dataId} AND attribute_type = '1';")
     List<IotDataDistribution> getMapping(String dataId);

     /**
      *   如果分发关系已经存在，更新权重
      * @param weight
      * @param dataId1
      * @param distributionTarget
      */
     @Update("UPDATE iot_data_distribution SET weight = #{weight}, attribute_type = 1 WHERE data_id = #{dataId1} AND target_data_id = #{distributionTarget};")
     void updateWeight(double weight, String dataId1, String distributionTarget);

     @Select("SELECT * FROM iot_data_distribution WHERE data_id = #{originalTarget};")
     List<IotDataDistribution> isHandout(String originalTarget);
}
